package com.zheng1.envii

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.accompanist.permissions.rememberPermissionState
import com.zheng1.envii.ui.screens.DeviceControlScreen
import com.zheng1.envii.ui.screens.DeviceManagerScreen
import com.zheng1.envii.ui.theme.EnvIITheme

sealed class Screen(val route: String) {
    object DeviceManagerScreen : Screen("DeviceManagerScreen")
    object DeviceControlScreen : Screen("DeviceControlScreen/{address}")
}

@OptIn(ExperimentalMaterial3Api::class)
class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalPermissionsApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)

        setContent {
            enableEdgeToEdge()
            EnvIITheme( dynamicColor = false ) {

                val navController = rememberNavController()
                NavHost(
                    navController = navController,
                    startDestination =  Screen.DeviceManagerScreen.route
                ){
                    composable(
                        route = Screen.DeviceManagerScreen.route
                    ) {
                        DeviceManagerScreen(
                            navController = navController,
                            viewModel = mainViewModel,
                        )
                    }
                    composable(
                        route = Screen.DeviceControlScreen.route,
                    ) { navBackStackEntry ->
                        DeviceControlScreen(
                            deviceAddress = navBackStackEntry.arguments?.getString("address")!!,
                            navController = navController,
                            viewModel = mainViewModel
                        )
                    }
                }
            }
        }
    }
}